import '../../../utils/json_converter.dart';

class AddBookReq {
  int? id;                         // 图书ID
  int? bookId;                     // 图书ID
  int? storeId;                    // 店铺ID
  String? isbn;                    // 国际标准书号
  String? name;                    // 图书名称
  String? imageUrl;                // 图书封面图片URL
  String? author;                  // 作者
  String? keyword;                 // 关键词，如"古典文学,中国文学,名著"
  String? publisher;               // 出版社
  double? referencePrice;          // 原价格
  double? purchasePrice;           // 进货价格
  double? salePrice;               // 销售价格
  double? discountPrice;           // 折扣价格
  int? inventory;                  // 库存数量
  int? categoryId;                 // 图书分类ID

  AddBookReq({
    this.id,
    this.bookId,
    this.storeId,
    this.isbn,
    this.name,
    this.imageUrl,
    this.author,
    this.keyword = '',
    this.publisher = '',
    this.referencePrice = 0.0,
    this.purchasePrice = 0.0,
    this.salePrice = 0.0,
    this.discountPrice = 0.0,
    this.inventory = 0,
    this.categoryId,
  });

  factory AddBookReq.fromJson(Map<String, dynamic> json) {
    return AddBookReq(
      id: JsonConverter.toInt(json['id']),
      bookId: JsonConverter.toInt(json['bookId']),
      storeId: JsonConverter.toInt(json['storeId']),
      isbn: JsonConverter.toStr(json['isbn']),
      name: JsonConverter.toStr(json['name']),
      imageUrl: JsonConverter.toStr(json['imageUrl']),
      author: JsonConverter.toStr(json['author']),
      keyword: JsonConverter.toStrWithDefault(json['keyword']),
      publisher: JsonConverter.toStrWithDefault(json['publisher']),
      referencePrice: JsonConverter.toDoubleWithDefault(json['referencePrice']),
      purchasePrice: JsonConverter.toDoubleWithDefault(json['purchasePrice']),
      salePrice: JsonConverter.toDoubleWithDefault(json['salePrice']),
      discountPrice: JsonConverter.toDoubleWithDefault(json['discountPrice']),
      inventory: JsonConverter.toIntWithDefault(json['inventory']),
      categoryId: JsonConverter.toInt(json['categoryId']),
    );
  }

  Map<String, dynamic> toJson() {
    final data = <String, dynamic>{};
    if (id != null) data['id'] = id;
    if (bookId != null) data['bookId'] = bookId;
    if (isbn != null) data['isbn'] = isbn;
    data['name'] = name;
    data['imageUrl'] = imageUrl;
    data['author'] = author;
    data['keyword'] = keyword;
    data['publisher'] = publisher;
    data['referencePrice'] = referencePrice;
    data['purchasePrice'] = purchasePrice;
    data['salePrice'] = salePrice;
    data['discountPrice'] = discountPrice;
    data['inventory'] = inventory;
    if (categoryId != null) data['categoryId'] = categoryId;
    data['storeId'] = storeId;
    return data;
  }
}
